class Solution:
    def reverseWords(self, s: str) -> str:
        s += ' '
        word_list = []
        temp = ''
        for i in range(len(s)):
            if len(temp) > 0 and s[i] == ' ':
                word_list.append(temp)
                temp = ''
            elif len(temp) == 0 and s[i] == ' ':
                continue
            else:
                temp += s[i]
        return ' '.join(word_list[::-1])


if __name__ == '__main__':
    x = Solution()
    print(x.reverseWords('the sky is blue'))